﻿namespace Digitalmes.Application.Common.BusinessPolicies;

/// <summary>
/// 策略注册器工厂。
/// </summary>
internal sealed class PolicyRegisterFactory(IServiceProvider serviceProvider) : IPolicyRegisterFactory
{
    private static readonly HashSet<string> Policies = [];

    /// <summary>
    /// 注册策略。
    /// </summary>
    public static void Register(string policyName)
    {
        if (!Policies.Add(policyName))
        {
            throw new InvalidOperationException($"已注册名称为 {policyName} 的业务策略，不能再重复注册。");
        }
    }

    public IBusinessPolicy? GetPolicy(string name)
    {
        return serviceProvider.GetKeyedService<IBusinessPolicy>(name);
    }
}
